import { useState, useCallback } from 'react';
import { CollapsibleItem } from '../types';

export const useCollapsibleItems = (initialItems: CollapsibleItem[]) => {
  const [items, setItems] = useState<CollapsibleItem[]>(initialItems);

  // 切换展开/收起状态
  const toggleItem = useCallback((itemId: string) => {
    setItems(prevItems => 
      prevItems.map(item => 
        item.id === itemId 
          ? { ...item, isExpanded: !item.isExpanded }
          : item
      )
    );
  }, []);

  // 展开特定项目
  const expandItem = useCallback((itemId: string) => {
    setItems(prevItems => 
      prevItems.map(item => 
        item.id === itemId 
          ? { ...item, isExpanded: true }
          : item
      )
    );
  }, []);

  // 展开所有项目
  const expandAll = useCallback(() => {
    setItems(prevItems => 
      prevItems.map(item => ({ ...item, isExpanded: true }))
    );
  }, []);

  // 收起所有项目
  const collapseAll = useCallback(() => {
    setItems(prevItems => 
      prevItems.map(item => ({ ...item, isExpanded: false }))
    );
  }, []);

  return {
    items,
    toggleItem,
    expandItem,
    expandAll,
    collapseAll
  };
};
